package ModelLayer;

import java.util.ArrayList;

public class DVDRegister
{
    private ArrayList<DVD>dvds;
    private static DVDRegister instance;

    /**
     * Constructor for objects of class DVDRegister
     */
    private DVDRegister()
     {
       dvds=new ArrayList<DVD>();
     }
     
     /**
     * Singleton.
     */
    
    public static DVDRegister getInstance(){
        
        if(instance==null){
            instance=new DVDRegister();
        }
        return instance;
    }
    
    
    
    /**
     * Add dvd.
     */
    
    public void addDVD(DVD dvd){
        
        dvds.add(dvd);
    }
    
    /**
     * Create dvd.
     */
    
    public boolean createDVD(long id,String title,String artist,String publicationDate){
       
        boolean found=false;
        boolean success=false;
       int index=0;
       while(index<dvds.size()&& !found){
           DVD a=dvds.get(index);
           if(id==a.getId()){
              found=true;
            }
            else{
            index++;
          }
         }
         if(found==false){
      DVD dvd=new DVD(id,title,artist,publicationDate);
       addDVD(dvd);
       System.out.println("DVD was created");
       success=true;
    }  
     else
         {
             System.out.println("There is already a DVD with that id");
            }
      return success;
     }
     
     
     /**
      * Read dvd.
      */
     
     
      public DVD findDVD(long id){
         
         boolean found=false;
         int index=0;
         int theIndex=0;
         while(index<dvds.size()&&!found)
         {
             DVD a =dvds.get(index);
             if(a.getId()==id){
                 found=true;
                 return a;
                }
                else{
                    index++;
                }
            }
            if(found==false){
               System.out.println("There is no DVD with that ID !");
             
            
         } 
         return null;
        }
        
        public void printDVD(long id)
        {
            DVD d = findDVD(id);
            if(d!=null)
            {
            d.print();
        }
        
        }
        
        
        /**
         * Update DVD.
         */
        
        
        public boolean updateDVDTitle(long id, String title)
   {
       int index=0;
       boolean found=false;
       while(index<dvds.size()&&!found)
       {
           {DVD p = dvds.get(index);
           
               if(p.getId()==id)
               {
                   found=true;
                   p.setTitle(title);
                   System.out.println("Title Updated Succesfully");
                   break;
                }
                else
                {
                    index++;
                }
            }
            
        }
        if (found==false)
        {
            System.out.println("There is no DVD with this ID");
        }
        return found;
    }
    
    
    
    
      public boolean updateDVDArtist(long id, String artist)
   {
       int index=0;
       boolean found=false;
       while(index<dvds.size()&&!found)
       {
           {DVD p = dvds.get(index);
           
               if(p.getId()==id)
               {
                   found=true;
                   p.setArtist(artist);
                   System.out.println("Artist Updated Succesfully");
                   break;
                }
                else
                {
                    index++;
                }
            }
            
        }
        if (found==false)
        {
            System.out.println("There is no DVD with this ID");
        }
        return found;
    }
    
    
    
    
    public boolean updateDVDpublicationDate(long id, String publicationDate)
   {
       int index=0;
       boolean found=false;
       while(index<dvds.size()&&!found)
       {
           {DVD p = dvds.get(index);
           
               if(p.getId()==id)
               {
                   found=true;
                   p.setPublicationDate(publicationDate);
                   System.out.println("publicationDate Updated Succesfully");
                   break;
                }
                else
                {
                    index++;
                }
            }
            
        }
        if (found==false)
        {
            System.out.println("There is no DVD with this ID");
        }
        
        return found;
    }
    
    
    
     /**
             * Delete DVD
             * 
             */
            
    public boolean removeDVD(long id)
    {
        int index = 0;
        boolean found=false;
        while(index<dvds.size()&&!found)
        {DVD p = dvds.get(index);
            if(p.getId()==id)
            {
                found=true;
                dvds.remove(index);
                System.out.println("DVD with id " + id + " was removed.");
                break;
            }
            else 
            {
                index++;
            }
        }
        if (found==false)
        {
            System.out.println("There was no DVD with this id");
        }
        return found;
    }
    
    
    
    /**
     * Create copy of the DVD.
     */
    
    public boolean createCopy(long id,String serialNumber,String purchaseDate,double purchasePrice){
         boolean found=false;
         int index=0;
         int theIndex=0;
         while(index<dvds.size()&&!found)
         {
             DVD a =dvds.get(index);
             if(a.getId()==id){
                 found=true;
                 dvds.get(index).createCopy(id,serialNumber,purchaseDate,purchasePrice);
                 System.out.println("The copy was created Succesfully !!");
                }
                else{
                    index++;
                }
            }
            if(found==false){
               System.out.println("There is no such DVD with that ID !");
             
            
         } 
         return found;
        }
        
        
        /**
         * List of all dvd
         */
       public void listAllDVDs()
       {
          System.out.println("ID   Title   Arist  Publication Date    Copies");
          for(DVD d : dvds)
          {
              System.out.print(d.getId() + "   " + d.getTitle() + "   " + d.getArtist() + "   ");
              System.out.print(d.getPublicationDate() + "    " + d.nrOfCopies());
              System.out.print("\n");
            }
        }
        
        /**
         * List of the copies of a certain DVD
         */
        public void listAllCopies(long id)
        {
            ArrayList<Copy> copies = findDVD(id).getListOfCopies();
            System.out.println("DVD ID  Serial Number  Purchase Date    Purchase Price    State");
          for(Copy c : copies)
          {
              String loaned = "Free";
              if(c.getLoanedState())
              {
                  loaned = "Loaned. Loan ID is " + c.getLoanId();
                }
              System.out.print(c.getDVDId() + "   " + c.getSerialNumber() + "   " + c.getPurchaseDate() + "   ");
              System.out.print(c.getPurchasePrice() + "    " + loaned);
              System.out.print("\n");
            }
            
        }
                
        
            
        
        public Copy findCopy(long dvdId)
        {
            int index=0;
            boolean found=false;
            while(index<dvds.size()&&!found)
            {
                DVD d = dvds.get(index);
                if(d.getId()==dvdId)
                {
                    if(d.nrOfCopies()==0)
                    {
                        System.out.println("There are no available copies of this DVD");
                        return null;
                    }
                    
                    return d.getCopy();
                }
                else index++;
            }
            return null;
           
        }
        
        
        
    
    
    
    
    
    
}

